// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.globalObjects;

import java.io.IOException;
import java.nio.ByteBuffer;
import se.krka.kahlua.vm.KahluaTable;
import se.krka.kahlua.vm.KahluaTableIterator;
import zombie.Lua.LuaManager;
import zombie.characters.IsoPlayer;
import zombie.core.logger.ExceptionLogger;
import zombie.core.network.ByteBufferWriter;
import zombie.debug.DebugLog;
import zombie.network.GameClient;
import zombie.network.GameServer;
import zombie.network.PacketTypes;
import zombie.network.TableNetworkUtils;
import zombie.spnetwork.SinglePlayerClient;

public final class CGlobalObjectNetwork {
    private static final ByteBuffer BYTE_BUFFER = ByteBuffer.allocate(1048576);
    private static final ByteBufferWriter BYTE_BUFFER_WRITER = new ByteBufferWriter(BYTE_BUFFER);
    private static KahluaTable tempTable;

    public static void receive(ByteBuffer bb) throws IOException {
        byte _byte = bb.get();
        switch (_byte) {
            case 1:
                receiveServerCommand(bb);
            case 2:
            default:
                break;
            case 3:
                receiveNewLuaObjectAt(bb);
                break;
            case 4:
                receiveRemoveLuaObjectAt(bb);
                break;
            case 5:
                receiveUpdateLuaObjectAt(bb);
        }
    }

    private static void receiveServerCommand(ByteBuffer byteBuffer) {
        String string0 = zombie.GameWindow.ReadString(byteBuffer);
        String string1 = zombie.GameWindow.ReadString(byteBuffer);
        boolean _boolean = byteBuffer.get() == 1;
        KahluaTable kahluaTable = null;
        if (_boolean) {
            kahluaTable = LuaManager.platform.newTable();

            try {
                TableNetworkUtils.load(kahluaTable, byteBuffer);
            } catch (Exception exception) {
                ExceptionLogger.logException(exception);
                return;
            }
        }

        CGlobalObjects.receiveServerCommand(string0, string1, kahluaTable);
    }

    private static void receiveNewLuaObjectAt(ByteBuffer byteBuffer) throws IOException {
        String string = zombie.GameWindow.ReadStringUTF(byteBuffer);
        int int0 = byteBuffer.getInt();
        int int1 = byteBuffer.getInt();
        byte _byte = byteBuffer.get();
        if (tempTable == null) {
            tempTable = LuaManager.platform.newTable();
        }

        TableNetworkUtils.load(tempTable, byteBuffer);
        CGlobalObjectSystem cGlobalObjectSystem = CGlobalObjects.getSystemByName(string);
        if (cGlobalObjectSystem != null) {
            cGlobalObjectSystem.receiveNewLuaObjectAt(int0, int1, _byte, tempTable);
        }
    }

    private static void receiveRemoveLuaObjectAt(ByteBuffer byteBuffer) {
        String string = zombie.GameWindow.ReadStringUTF(byteBuffer);
        int int0 = byteBuffer.getInt();
        int int1 = byteBuffer.getInt();
        byte _byte = byteBuffer.get();
        CGlobalObjectSystem cGlobalObjectSystem = CGlobalObjects.getSystemByName(string);
        if (cGlobalObjectSystem != null) {
            cGlobalObjectSystem.receiveRemoveLuaObjectAt(int0, int1, _byte);
        }
    }

    private static void receiveUpdateLuaObjectAt(ByteBuffer byteBuffer) throws IOException {
        String string = zombie.GameWindow.ReadStringUTF(byteBuffer);
        int int0 = byteBuffer.getInt();
        int int1 = byteBuffer.getInt();
        byte _byte = byteBuffer.get();
        if (tempTable == null) {
            tempTable = LuaManager.platform.newTable();
        }

        TableNetworkUtils.load(tempTable, byteBuffer);
        CGlobalObjectSystem cGlobalObjectSystem = CGlobalObjects.getSystemByName(string);
        if (cGlobalObjectSystem != null) {
            cGlobalObjectSystem.receiveUpdateLuaObjectAt(int0, int1, _byte, tempTable);
        }
    }

    private static void sendPacket(ByteBuffer byteBuffer) {
        if (GameServer.bServer) {
            throw new IllegalStateException("can't call this method on the server");
        } else {
            if (GameClient.bClient) {
                ByteBufferWriter byteBufferWriter0 = GameClient.connection.startPacket();
                byteBuffer.flip();
                byteBufferWriter0.bb.put(byteBuffer);
                PacketTypes.PacketType.GlobalObjects.send(GameClient.connection);
            } else {
                ByteBufferWriter byteBufferWriter1 = SinglePlayerClient.connection.startPacket();
                byteBuffer.flip();
                byteBufferWriter1.bb.put(byteBuffer);
                SinglePlayerClient.connection.endPacketImmediate();
            }
        }
    }

    public static void sendClientCommand(IsoPlayer isoPlayer, String string0, String string1, KahluaTable kahluaTable) {
        BYTE_BUFFER.clear();
        writeClientCommand(isoPlayer, string0, string1, kahluaTable, BYTE_BUFFER_WRITER);
        sendPacket(BYTE_BUFFER);
    }

    private static void writeClientCommand(IsoPlayer isoPlayer, String string0, String string1, KahluaTable kahluaTable, ByteBufferWriter byteBufferWriter) {
        PacketTypes.PacketType.GlobalObjects.doPacket(byteBufferWriter);
        byteBufferWriter.putByte((byte)(isoPlayer != null ? isoPlayer.PlayerIndex : -1));
        byteBufferWriter.putByte((byte)2);
        byteBufferWriter.putUTF(string0);
        byteBufferWriter.putUTF(string1);
        if (kahluaTable != null && !kahluaTable.isEmpty()) {
            byteBufferWriter.putByte((byte)1);

            try {
                KahluaTableIterator kahluaTableIterator = kahluaTable.iterator();

                while (kahluaTableIterator.advance()) {
                    if (!TableNetworkUtils.canSave(kahluaTableIterator.getKey(), kahluaTableIterator.getValue())) {
                        DebugLog.log("ERROR: sendClientCommand: can't save key,value=" + kahluaTableIterator.getKey() + "," + kahluaTableIterator.getValue());
                    }
                }

                TableNetworkUtils.save(kahluaTable, byteBufferWriter.bb);
            } catch (IOException iOException) {
                ExceptionLogger.logException(iOException);
            }
        } else {
            byteBufferWriter.putByte((byte)0);
        }
    }

    public static void Reset() {
        if (tempTable != null) {
            tempTable.wipe();
            tempTable = null;
        }
    }
}
